package com.hjw.config;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.SaTokenDaoRedis;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForMixin;

import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Administrator
 */
@Slf4j
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer  {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new SaInterceptor( e -> {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            log.info("请求地址：{}", request.getRequestURI());
            log.info("PUBLIC_URL:{}", request.getLocalAddr());
            log.info("PUBLIC_URL:{}", request.getLocalPort());
            log.info("PUBLIC_URL:{}", request.getLocalName());
                }))
                .addPathPatterns("/**")
                .excludePathPatterns("/api/public/**")
                .excludePathPatterns("/**.html")
                .excludePathPatterns("/alipay/return")
                .excludePathPatterns("/login/**");
    }

    @Bean
    public StpLogic getStpLogicJwt() {
        return new StpLogicJwtForSimple();
    }

    @Bean
    public SaTokenDao saTokenDao(RedisConnectionFactory redisConnectionFactory) {
        SaTokenDaoRedis saTokenDaoRedis = new SaTokenDaoRedis();
        saTokenDaoRedis.init(redisConnectionFactory);
        return saTokenDaoRedis;
    }

}